package uit.hcm.object;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Location;
import android.media.MediaPlayer;
import android.text.Html;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Projection;

public class MapOverlayCustom extends com.google.android.maps.Overlay {

	private GeoPoint _currentPoint;
	private MapActivity _activity;
	private long _timer;
	private float _radius = 6;
	private Paint _paint = new Paint();
	private List<DiemDuLich> listTravelLocation;

	public MapOverlayCustom(GeoPoint pointCurrent, MapActivity activity,
			List<DiemDuLich> list) {

		this.listTravelLocation = list;
		this._currentPoint = pointCurrent;
		this._activity = activity;
		this._timer = System.currentTimeMillis();

	}

	public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
			long when) {
		super.draw(canvas, mapView, shadow);
		location(canvas, mapView);
		travelLocation(canvas, mapView);
		return true;
	}

	public void location(Canvas canvas, MapView mapView) {
		Point screenPts = new Point();
		Projection projection = mapView.getProjection();
		projection.toPixels(_currentPoint, screenPts);
		if (System.currentTimeMillis() > _timer + 100) {
			_paint.setColor(Color.RED);
			canvas.drawCircle(screenPts.x, screenPts.y, _radius, _paint);

		}
	}

	public void travelLocation(Canvas canvas, MapView mapView) {

		if (listTravelLocation.size() > 0) {
			try {
				Iterator<DiemDuLich> listPoint = listTravelLocation.iterator();
				while (listPoint.hasNext()) {
					DiemDuLich lo = listPoint.next();
					GeoPoint point = new GeoPoint((int) (lo.getViDo() * 1E6),
							(int) (lo.getKinhDo() * 1E6));
					Point p = new Point();
					Projection projection = mapView.getProjection();
					projection.toPixels(point, p);
					Paint pa = new Paint();

					pa.setColor(Color.BLUE);
					if (distance(_currentPoint, point) <= 300) {
						if (System.currentTimeMillis() > _timer + 40) {
							canvas.drawCircle(p.x, p.y, 4, pa);
						}
					}
					
				}
			} catch (Exception e) {
				// TODO: handle exception
			}

		}

	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {

		if (listTravelLocation.size() > 0) {

			GeoPoint pointClick = mapView.getProjection().fromPixels(
					(int) event.getX(), (int) event.getY());
			for (int i = 0; i < listTravelLocation.size(); i++) {

				DiemDuLich lo = listTravelLocation.get(i);
				GeoPoint point = new GeoPoint((int) (lo.getViDo() * 1E6),
						(int) (lo.getKinhDo() * 1E6));
				String strInfo = "- ";
					strInfo = strInfo
							+ Html.fromHtml("<font color='#CD7F32'>"
									+ lo.getTenDiem() + "</font>")
									+ "\n-Giới thiệu: " + lo.getGioiThieu();
							
				
				if (distance(pointClick, point) <= 20) {
					if (event.getAction() == 1) {
						ShowOverlay show = new ShowOverlay(point, _activity);
						show.showView(mapView, strInfo);
					}
				}
			}

		}
		return false;
	}
	public int distance(GeoPoint p1, GeoPoint p2) {
		Double lat1 = p1.getLatitudeE6() / 1E6;
		Double log1 = p1.getLongitudeE6() / 1E6;
		Location location1 = new Location("location1");
		location1.setLatitude(lat1);
		location1.setLongitude(log1);

		Double lat2 = p2.getLatitudeE6() / 1E6;
		Double log2 = p2.getLongitudeE6() / 1E6;
		Location location2 = new Location("location1");
		location2.setLatitude(lat2);
		location2.setLongitude(log2);

		int distanceTwoPoint = Math.round(location1.distanceTo(location2));
		return distanceTwoPoint;
	}

}
